package com.wang.tool.task;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;

import java.util.Date;

/**
 * @Description 任务
 * @Author shaopeng
 * @Date 2023/6/2
 * @Version 1.0
 */
public class Task extends TaskBase {

    private static final long serialVersionUID = -7755155340743658308L;

    /**
     * 任务id
     */
    private Long id;

    /**
     * 状态码
     */
    private TaskStatus status;

    /**
     * 是否已触发关联方
     */
    private boolean fire;

    /**
     * 业务状态码
     * handler自定义 为空都没事儿
     */
    private String bizStatus;

    /**
     * 自检验证时间点
     * 扫表/消息 到点触发
     */
    private Date checkTime;

    /**
     * 外部id
     * 任务实际执行是第三方的，所以这里可以留着第三方的id，后面自检查询时用
     */
    private String outId;


    /**
     * 任务模型
     * 任务运行进度如何了之类的，json字符串，每个handler自定义
     */
    private JSONObject model;


    /**
     * 创建时间
     */
    private Date gmtCreate;

    /**
     * 变更时间
     */
    private Date gmtModified;

    /**
     * 处理次数
     * 初始0
     * 每次操作+1
     */
    private int handleTimes;


    /**
     * Class 或者  TypeReference.getType()
     *
     * @param type
     * @param <T>
     * @return
     */
    public <T> T modelTo(Class<T> type) {
        if (model == null) {
            return null;
        }
        return JSON.parseObject(model.toJSONString(), type);
    }

    public <T> T modelTo(TypeReference<T> type) {
        if (model == null) {
            return null;
        }
        return JSON.parseObject(model.toJSONString(), type);
    }


    public TaskStage toStage() {
        TaskStage stage = new TaskStage();
        stage.setId(id);
        stage.setCheckTime(checkTime);
        stage.setModel(model);
        stage.setOutId(outId);
        stage.setBizStatus(bizStatus);
        return stage;
    }

    /**
     * 是否结束
     *
     * @return
     */
    public boolean judgeIsComplete() {
        return status == TaskStatus.SUCCESS || status == TaskStatus.FAIL;
    }


    public TaskStatus getStatus() {
        return status;
    }

    public void setStatus(TaskStatus status) {
        this.status = status;
    }

    public String getBizStatus() {
        return bizStatus;
    }

    public void setBizStatus(String bizStatus) {
        this.bizStatus = bizStatus;
    }

    public Date getCheckTime() {
        return checkTime;
    }

    public void setCheckTime(Date checkTime) {
        this.checkTime = checkTime;
    }

    public String getOutId() {
        return outId;
    }

    public void setOutId(String outId) {
        this.outId = outId;
    }

    public Date getGmtCreate() {
        return gmtCreate;
    }

    public void setGmtCreate(Date gmtCreate) {
        this.gmtCreate = gmtCreate;
    }

    public Date getGmtModified() {
        return gmtModified;
    }

    public void setGmtModified(Date gmtModified) {
        this.gmtModified = gmtModified;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public JSONObject getModel() {
        return model;
    }

    public void setModel(JSONObject model) {
        this.model = model;
    }

    public int getHandleTimes() {
        return handleTimes;
    }

    public void setHandleTimes(int handleTimes) {
        this.handleTimes = handleTimes;
    }

    public boolean isFire() {
        return fire;
    }

    public void setFire(boolean fire) {
        this.fire = fire;
    }
}
